package com.aurora.leetcode.util;

import com.aurora.leetcode.entity.ListNode;

/**
 * @author : Aurora
 * @date : 4/11/24 3:59 AM
 * @description
 */
public class MyUtils {

    /**
     * 打印一维数组
     *
     * @param arr
     */
    public static <T>void printArray(int[] arr) {
        for (int i=0; i<arr.length; ++i) {
            if(i == arr.length -1){
                System.out.println(arr[i]);
            }else{
                System.out.print(arr[i]+",");
            }
        }
    }

    public static <T>void printCharArray(char[] arr) {
        for (int i=0; i<arr.length; ++i) {
            if(i == arr.length -1){
                System.out.println(arr[i]);
            }else{
                System.out.print(arr[i]+",");
            }
        }
    }

    /**
     * 打印二维数组
     * @param arr
     */
    public static void printArray(int[][] arr){
        for(int i=0; i<arr.length; ++i){
            for(int j=0; j<arr[i].length; ++j){
                if(j<arr[i].length -1){
                    System.out.print(arr[i][j]+",");
                }else{
                    System.out.println(arr[i][j]);
                }
            }
        }
    }

    /**
     * 根据数组构造单链表
     * @param arr
     * @return
     */
    public static ListNode constructListNode(int[] arr){
        ListNode head = new ListNode();
        ListNode p = head;
        for (int i : arr) {
            ListNode node = new ListNode(i);
                p.next = node;
                p = p.next;
        }
        return head.next;
    }

    /**
     * 打印单链表
     * @param head
     */
    public static void printLinkedList(ListNode head){
        ListNode p = head;
        while(p != null){
            if(p.next != null){
                System.out.print(p.val+",");
            }else{
                System.out.println(p.val);
            }
            p = p.next;
        }
    }


    /**
     * 头插法
     * @param head
     * @param insert
     */
    public static void headInsert(ListNode head, ListNode insert){
        if(head.next == null){
            head.next = insert;
        }else{
            ListNode temp = head.next;
            head.next = insert;
            insert.next = temp;
        }
    }


}
